import numpy as np
import pandas as pd
from flask import Flask
from flask_cors import CORS
from flask import request
import Recommend.GoodsRecommendByItem as ReByItem

#代表一个flask对象
app=Flask(__name__)
CORS(app, resources={r"/*": {"origins": "*"}})

#  基于商品相似度的商品推荐
@app.route("/RecommendedByItem")
def goodsRecommendByItem():
    # 读取数据
    dtype = {"userid": np.int32, "movieId": np.int32, "rating": np.float32}
    ratings = pd.read_csv(r"D:\Project\FootShopping\Product-Recommended\data\ratings_data.csv", dtype=dtype,
                          usecols=range(3))

    # 创建评分矩阵和相似度矩阵
    ratings_matrix = ratings.pivot_table(index=["userId"], columns=["movieId"], values="rating")
    # 物品相似度矩阵
    similarity = ratings_matrix.corr()

    uid = int(request.args["id"])
    k = 20  # 推荐20个物品
    recommended_item_ids = ReByItem.recommendItemsByItem(uid, ratings_matrix, similarity, k)
    print(recommended_item_ids)
    return str(recommended_item_ids)


if __name__ == '__main__':
    app.run(host="127.0.0.1",port=8080)